package com.zhaojonhy.zhaopangwb.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.bean.UserItem;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/14.
 */

public class UserItemAdapter extends BaseAdapter {
    final static String TAG = UserItemAdapter.class.getSimpleName() ;
    private Context context;
    private ArrayList<UserItem> datas;

    public UserItemAdapter( Context context,ArrayList<UserItem> datas) {
        Log.d(TAG,"datas:"+datas.size()) ;
        this.context = context ;
        this.datas = datas ;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public UserItem getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null) {
            holder = new Holder();
            convertView = View.inflate(context, R.layout.item_user, null);
            holder.v_divider = convertView.findViewById(R.id.v_divider);
            holder.ll_content = convertView.findViewById(R.id.ll_content);
            holder.iv_left = (ImageView) convertView.findViewById(R.id.iv_left);
            holder.tv_subhead = (TextView) convertView.findViewById(R.id.tv_subhead);
            holder.tv_caption = (TextView) convertView.findViewById(R.id.tv_caption);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        //set data
        Log.d(TAG,"datas:"+datas.size()) ;
        UserItem item = getItem(position);
        Log.d(TAG,"datas:"+item.getLeftImg()) ;
        holder.iv_left.setImageResource(item.getLeftImg());
        holder.tv_subhead.setText(item.getSubhead());
        holder.tv_caption.setText(item.getCaption());

        holder.v_divider.setVisibility(item.isShowTopDivider() ?
                View.VISIBLE : View.GONE);

        holder.ll_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showToast(context, "item click position = " + position, Toast.LENGTH_SHORT);
            }
        });

        return convertView;
    }

    class Holder{
        private View v_divider ;
        private View ll_content ;
        private ImageView iv_left ;
        private TextView tv_subhead ;
        private TextView tv_caption ;
    }
}
